<?php

if (!defined("EXPONENT")) exit("");

$gallery = null;
if (isset($_GET['id'])) $gallery = $db->selectObject("imagegallery_gallery","id=".intval($_GET['id']));
// get module title
if ($gallery) {
$module_container=$db->selectObject('container',"internal = '".serialize($loc)."'");
if (!$module_container)
	$module_container->title='Hình ảnh';
	
  $max_item = $gallery->perrow * $gallery->perpage;
	// get all gallery here
	$count_post = $db->countObjects('imagegallery_image',"gallery_id = {$gallery->id}");
		
	// page split
	// cal how many posts
	$page_count=floor($count_post / $max_item);
	if ($count_post % $max_item != 0)
	  $page_count++;
		
	// get page
	$page=1;
	if (isset($_REQUEST['page']))  $page=intval($_REQUEST['page']);
  if (($page<1) || ($page>$page_count)) $page=1;
  $start_pos=($page-1)*$max_item;

	exponent_flow_set(SYS_FLOW_PUBLIC,SYS_FLOW_ACTION);
  
  // get first image
  $temp=$db->selectObject("imagegallery_image","gallery_id = {$gallery->id}");
	$image=$temp;
	if ($image){
	$temp=$db->selectObject("file","id = {$image->file_id}");
		$file_path=$temp->directory ."/". $temp->filename;
		$gallery->image_path=$file_path;
	}
	else {$gallery->image_path="";}
  
	$loc = unserialize($gallery->location_data);
	
	$images = $db->selectObjects("imagegallery_image","gallery_id=".$gallery->id . " ORDER BY rank ASC LIMIT {$start_pos},{$max_item}");
	for ($i = 0; $i < count($images); $i++) {
		$images[$i]->file = $db->selectObject("file","id=".$images[$i]->file_id);
	}	
	$gallery->images = $images;
	$gallery->image_count=$count_post;

	$iloc = exponent_core_makeLocation($loc->mod,$loc->src);
	$iloc->int = $gallery->id;
	$gallery->permissions = array(
		"administrate"=>exponent_permissions_check("administrate",$iloc),
		"edit"=>exponent_permissions_check("edit",$iloc),
		"delete"=>exponent_permissions_check("delete",$iloc),
		"manage"=>exponent_permissions_check("manage",$iloc)
	);
	//$template = new template("imagegallerymodule","_view_gallery",$iloc);
	$template = new template("imagegallerymodule","_view_all_galleries",$iloc);
	$template->register_permissions(
		array("administrate","edit","delete","manage"),
		$iloc
	);

	$template->assign("gallery",$gallery);
	$template->assign("page",$page);
	$template->assign("page_count",$page_count);
	$template->assign("moduletitle",$module_container->title);
	$template->output();
	
	_ab_theme_action_register_title($gallery->name, $page, 0);
	_ab_theme_action_register_title(strip_tags($gallery->description), 1, 1);
} else {
	echo SITE_404_HTML;
}

?>
